﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Model.Admin;
using Xproject.Framework.Core.Repositories;
using Xproject.Framework.Utility.Extensions;

namespace Repository.WXProjectAdmin
{
   public static class AdvertisementRep
    {
        public static Advertisement Get(this IRepositoryAsync<Advertisement> repository,string site, string id)
        {
            Expression<Func<Advertisement, bool>> express = i => i.Site == site;
            if (!string.IsNullOrEmpty(id))
            {
                express = express.AndAlso(i => i.ID == id);
            }
            return repository.Query().Where(express).Select().OrderByDescending(i => i.CreateTime).FirstOrDefault();
        }

        public static List<Advertisement> GetAllAdvertisements(this IRepositoryAsync<Advertisement> repository, string site)
        {
            Expression<Func<Advertisement, bool>> express = i => i.Site == site;
            return repository.Query().Where(express).Select().OrderByDescending(i => i.CreateTime).ToList();
        }

        public static List<Advertisement> GetAdvertisementList(this IRepositoryAsync<Advertisement> repository, string id, string site, string adName, string adType, string remarks, string keyWord,
            DateTime? startTime, DateTime? endTime, string order, string ordername)
        {
            Expression<Func<Advertisement, bool>> express = i => i.Site==site ;
            if (!string.IsNullOrEmpty(id))
            {
                express = express.AndAlso(i => i.ID == id);
            }
            if (!string.IsNullOrEmpty(adName))
            {
                express = express.AndAlso(i => i.AdName == adName);
            }
            if (!string.IsNullOrEmpty(adType))
            {
                express = express.AndAlso(i => i.AdType == adType);
            }
            if (!string.IsNullOrEmpty(remarks))
            {
                express = express.AndAlso(i => i.Remarks == remarks);
            }
            if (!string.IsNullOrEmpty(keyWord))
            {
                express = express.AndAlso(i => i.KeyWord == keyWord);
            }
            return repository.Query().Where(express).Select().OrderByDescending(i => i.CreateTime).ToList();
             
        }

    }
}
